//
//  ZH_HomeViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/13.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_HomeViewController.h"
#import "GC_HPBannerTableViewCell.h"
#import "ZH_FeaturesSelectTableViewCell.h"
#import "ZH_ScrollViewTitleTableViewCell.h"
#import "ZH_HomeImgaeTableViewCell.h"
#import "ZH_HomeTopTitleTableViewCell.h"
#import "ZH_HomeCourceTableViewCell.h"
#import "ZH_InstitutionsTableViewCell.h"
#import "ZH_PlayBackTableViewCell.h"
#import "ZH_HomeInformationTableViewCell.h"
#import "ZH_HomeClassViewController.h"
#import "ZH_HomeLiveViewController.h"
#import "ZH_HomeVideoViewController.h"
#import "ZH_HomeMessageViewController.h"
#import "ZH_HomeSourceInfoViewController.h"
#import "ZH_HomeScheduleViewController.h"
#import "WSAnswerDisplayViewController.h"
#import "WSNewsViewController.h"
#import "ZH_HomeViewModel.h"
@interface ZH_HomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,strong)ZH_HomeViewModel *viewModel;
@property (nonatomic,strong)NSMutableArray *bannerList;
//推荐课程
@property (nonatomic,strong)NSMutableArray *courseList;
//优选机构
@property (nonatomic,strong)NSMutableArray *mechanismList;
@property (nonatomic,strong)UIButton *leftButton;
@end

@implementation ZH_HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
}
- (void)cleanUpData{
    [self setViewModel:nil];
    [self setBannerList:nil];
    [self.bannerList removeAllObjects];
    [self setCourseList:nil];
    [self.courseList removeAllObjects];
    [self setMechanismList:nil];
    [self.mechanismList removeAllObjects];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self initData];
    //首页的资讯
    [self initDatas];
}

- (void)initData{
    
    WEAKSelf;
    [self.viewModel homeResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [weakSelf.bannerList removeAllObjects];
            [weakSelf.courseList removeAllObjects];
            [weakSelf.mechanismList removeAllObjects];
            [self.bannerList addObjectsFromArray:data[@"data"][@"banner_list"]];
            [self.courseList addObjectsFromArray:data[@"data"][@"course_list"]];
            [self.mechanismList addObjectsFromArray:data[@"data"][@"mechanism_list"]];
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
- (void)initDatas{
    WEAKSelf;
    [self.viewModel informationResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            weakSelf.dic = data[@"data"][@"top_article"];
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
- (void)loadNavigationBackButton{
    
}
///更新导航栏显示
- (void)loadNavigationBarBackground{
    [((Hen_BaseNavigationViewController*)self.navigationController) setNavigationBarBgColor:[UIColor clearColor]];
    [((Hen_BaseNavigationViewController*)self.navigationController) setBackgroundHidden:YES];
    [((Hen_BaseNavigationViewController*)self.navigationController) setShadowViewHidden:YES];
}

- (void)initUserInterface{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_msg1"] style:UIBarButtonItemStylePlain target:self action:@selector(order)];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(-[DATAMODEL KNavigationBarHeioght]);
        make.left.right.bottom.equalTo(self.view);
    }];
}
- (void)order{
    ZH_HomeMessageViewController *vc = [[ZH_HomeMessageViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 7;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 2) {
        return 2;
    }
    else if (section == 3){
        return self.courseList.count + 1;
    }
    else if (section == 4){
        return 2;
    }
    else if (section == 5){
        return 3;
    }
    else if (section == 6){
        return 2;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return FITSCALE(9);
    }
    else if (section == 2){
        return FITSCALE(9);
    }
    else if (section == 5){
        return FITSCALE(9);
    }
    else if (section == 6){
        return FITSCALE(9);
    }
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        GC_HPBannerTableViewCell *cell = [GC_HPBannerTableViewCell cellWithTableView:tableView];
        [cell setPicHeight:FITSCALE(201)];
        [cell updateUIForImages:self.bannerList andDefaultImage:@"img_banner"];
        return cell;
    }
    else if (indexPath.section == 1){
        ZH_FeaturesSelectTableViewCell *cell = [ZH_FeaturesSelectTableViewCell cellWithTableView:tableView];
        [cell updateUiForData:@[@[@"icon_kc",@"课程"],@[@"icon_live",@"直播"],@[@"icon_sp",@"视频"],@[@"icon_zxs",@"资讯"],@[@"icon_kb",@"课表"]].mutableCopy];
        WEAKSelf;
        cell.onButtonClick = ^(NSInteger index) {
            if (index == 0) {
                ZH_HomeClassViewController *vc = [[ZH_HomeClassViewController alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
            else if (index == 1){
                ZH_HomeLiveViewController *vc = [[ZH_HomeLiveViewController alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
            else if (index == 2){
                ZH_HomeVideoViewController *vc = [[ZH_HomeVideoViewController alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
            else if (index == 3) {
                UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"WSTeacher" bundle:nil];
                WSNewsViewController *newsVc = [storyBoard instantiateViewControllerWithIdentifier:[WSNewsViewController className]];
                [self.navigationController pushViewController:newsVc animated:YES];
            }
            else if (index == 4){
                ZH_HomeScheduleViewController *vc = [[ZH_HomeScheduleViewController alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
        };
        return cell;
    }
    else if (indexPath.section == 2){
        if (indexPath.row == 0) {
            ZH_ScrollViewTitleTableViewCell *cell = [ZH_ScrollViewTitleTableViewCell cellWithTableView:tableView];
            return cell;
        }
        else if (indexPath.row == 1){
            ZH_HomeImgaeTableViewCell *cell = [ZH_HomeImgaeTableViewCell cellWithTableView:tableView];
            cell.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
            return cell;
        }
    }
    else if (indexPath.section == 3){
        if (indexPath.row == 0) {
            ZH_HomeTopTitleTableViewCell *cell = [ZH_HomeTopTitleTableViewCell cellWithTableView:tableView];
            cell.titleString = @"推荐课程";
            return cell;
        }
        else{
            ZH_HomeCourceTableViewCell *cell = [ZH_HomeCourceTableViewCell cellWithTableView:tableView];
            [cell updateUI:self.courseList[indexPath.row - 1]];
            return cell;
        }
    }
    else if (indexPath.section == 4){
        if (indexPath.row == 0) {
            ZH_HomeTopTitleTableViewCell *cell = [ZH_HomeTopTitleTableViewCell cellWithTableView:tableView];
            cell.titleString = @"优质机构";
            return cell;
        }
        else if (indexPath.row == 1){
            ZH_InstitutionsTableViewCell *cell = [ZH_InstitutionsTableViewCell cellWithTableView:tableView];
            [cell updateUiForData:self.mechanismList];
            return cell;
        }
    }
    else if(indexPath.section == 5){
        if (indexPath.row == 0) {
            ZH_HomeTopTitleTableViewCell *cell = [ZH_HomeTopTitleTableViewCell cellWithTableView:tableView];
            cell.titleString = @"直播回放";
            return cell;
        }
        else{
            ZH_PlayBackTableViewCell *cell = [ZH_PlayBackTableViewCell cellWithTableView:tableView];
            return cell;
        }
    }
    else if (indexPath.section == 6){
        if (indexPath.row == 0) {
            ZH_HomeTopTitleTableViewCell *cell = [ZH_HomeTopTitleTableViewCell cellWithTableView:tableView];
            cell.titleString = @"资讯";
            return cell;
        }
        else{
            ZH_HomeInformationTableViewCell *cell = [ZH_HomeInformationTableViewCell cellWithTableView:tableView];
            [cell updateUiForData:self.dic];
            return cell;
        }
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 3) {
        if (indexPath.row != 0) {
            ZH_HomeSourceInfoViewController *vc = [[ZH_HomeSourceInfoViewController alloc] init];
            vc.idString = [NSString stringWithFormat:@"%@",self.courseList[indexPath.row - 1][@"id"]];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    else if (indexPath.section == 2){
        if (indexPath.row == 1) {
            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"WSTeacher" bundle:nil];
            WSAnswerDisplayViewController *answerVc = [storyBoard instantiateViewControllerWithIdentifier:[WSAnswerDisplayViewController className]];
            [self.navigationController pushViewController:answerVc animated:YES];
            
        }
    }
}
#pragma mark - getter,setter
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonBackgroudColor;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}

- (ZH_HomeViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_HomeViewModel alloc] init];
    }
    return _viewModel;
}
- (NSMutableArray *)bannerList{
    if (!_bannerList) {
        _bannerList = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _bannerList;
}
- (NSMutableArray *)courseList{
    if (!_courseList) {
        _courseList = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _courseList;
}
//优选机构
- (NSMutableArray *)mechanismList{
    if (!_mechanismList) {
        _mechanismList = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _mechanismList;
}
@end
